package com.student.manager.service.impl;

import com.student.manager.bean.PageBean;
import com.student.manager.bean.Student;
import com.student.manager.dao.StudentDao;
import com.student.manager.dao.impl.StudentDaoImpl;
import com.student.manager.service.StudentService;

import java.sql.SQLException;
import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public List<Student> findAllStudent() throws SQLException {
        StudentDao studentDao = new StudentDaoImpl();
        return studentDao.findAllStudents();
    }

    @Override
    public PageBean findStudentByPage(int currentPage) throws SQLException {
        PageBean<Student> pageBean = new PageBean<Student>();
        int pageSize = StudentDao.PAGE_SIZE;
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);

        StudentDao dao = new StudentDaoImpl();
        List<Student> students = dao.findStudentByPage(currentPage);
        pageBean.setList(students);

        int count = dao.findCount();
        pageBean.setTotalSize(count);

        pageBean.setTotalPage((int) Math.ceil(count * 1.0 / pageSize));
        return pageBean;
    }

    @Override
    public Student findStudentById(int sid) throws SQLException {
        StudentDao dao = new StudentDaoImpl();
        return dao.findStudentById(sid);
    }

    @Override
    public List<Student> searchStudent(String sname, String sgender) throws SQLException {
        StudentDao dao = new StudentDaoImpl();
        return dao.likeSearchStudent(sname, sgender);
    }

    @Override
    public void insert(Student student) throws SQLException {
        StudentDao dao = new StudentDaoImpl();
        dao.insert(student);
    }

    @Override
    public void delete(int sid) throws SQLException {
        StudentDao dao = new StudentDaoImpl();
        dao.delete(sid);
    }

    @Override
    public void update(Student student) throws SQLException {
        StudentDao dao = new StudentDaoImpl();
        dao.update(student);
    }
}
